<?php

namespace Home\Controller;

use Think\Controller;
use Think\Model;
use Home\Model\ContentCollectModel;

class AlarmSettingController extends Controller {
	
	// 报警设置list
	public function alarmSettingList() {
		if (session ( 'name' )) {
			$alarmSettingTable = M ( 'AlertRule' );
			if (! empty ( I ( 'param.page', '' ) ) && ! empty ( I ( 'param.pageCount', '' ) )) {
				$alarmSettingTable->page ( I ( 'param.page' ), I ( 'param.pageCount' ) );
			}
			$alarmSetting = $alarmSettingTable->select ();
			$taskTable = M ( 'Task' );
			for($i = 0; $i < (count ( $alarmSetting )); $i ++) {
				$map = array ();
				$task_id_arr = explode ( ',', $alarmSetting [$i] ['task_id'] );
				foreach ( $task_id_arr as $tid ) {
					$map [] ['id'] = $tid;
				}
				$map ['_logic'] = 'OR';
				$taskArr = $taskTable->where ( $map )->select ();
				foreach ( $taskArr as $task ) {
					$alarmSetting [$i] ['task_name'] [] = $task ['task_name'];
				}
			}
			echo json_encode ( array (
					'result' => $alarmSetting,
					'count' => $alarmSettingTable->count(),
					'code' => 1 
			) );
		} else {
			echo json_encode ( array (
					'result' => '登录失效重新登陆',
					'code' => 0 
			) );
		}
	}
	
	//报警设置添加
	public function addAlarmSetting() {
		if (session ( 'name' )) {
			$alarmSettingTable = M ( 'AlertRule' );
			$data ['alarm_name'] = I ( 'param.alarm_name', '' );
			$data ['task_id'] = I ( 'param.task_id', '' );
			$data ['rule_alert'] = I ( 'param.rule_alert', '' );
			$data ['enabled'] = I ( 'param.enabled', 1 );
			$data ['alarm_way'] = I ( 'param.alarm_way', 1 );
			$map['alarm_name'] = $data ['alarm_name'];
			if(empty($data ['alarm_name'])){
				die(json_encode ( array ('result' => '报警名称不能为空',	'code' => 0)));
			}
			if(empty($data ['task_id'])){
				die(json_encode ( array ('result' => '任务不能为空',	'code' => 0)));
			}
			if(empty($data ['rule_alert'])){
				die(json_encode ( array ('result' => '报警规则不能为空',	'code' => 0)));
			}
			$alarmSetting = $alarmSettingTable->where ( $map )->select ();
			if ($alarmSetting) {
				echo json_encode ( array (
						'result' => '名称重复',
						'code' => 0 
				) );
				exit ();
			}
			$alarmSettingTable->add ( $data );
			echo json_encode ( array (
					'result' => '添加成功',
					'code' => 1 
			) );
		} else {
			echo json_encode ( array (
					'result' => '登录失效重新登陆',
					'code' => 0 
			) );
		}
	}
	
	//报警设置修改
	public function updateAlarmSetting(){
		if(session('name')){
			$alarmSettingTable = M ( 'AlertRule' );
			$map['id'] = I('param.id');
			$data=array();
			if(!empty(I('param.alarm_name',''))){
				$data['alarm_name'] = I('param.alarm_name','');
				$mymap['alarm_name'] = $data ['alarm_name'];
				$mymap['id'] = array('neq',I('param.id'));
				$alarmSetting = $alarmSettingTable->where ( $mymap )->select ();
				if ($alarmSetting) {
					echo json_encode ( array ( 
							'result' => '名称重复',
							'code' => 0
					) );
					exit ();
				}
			}
			if(I('param.alarm_way','') !=null){
				$data['alarm_way'] = I('param.alarm_way','');
			}
			if(!empty(I('param.task_id',''))){
				$data['task_id'] = I('param.task_id','');
			}
			if(!empty(I('param.rule_alert',''))){
				$data['rule_alert'] = I('param.rule_alert','');
			}
			if(I('param.exact','') !=null){
				$data['exact'] = I('param.exact','');
			}
			if(I('param.enabled','') != null){
				$data['enabled'] = I('param.enabled','');
			}
			if(!empty(I('param.alarm_name',''))){
    			$data['alarm_name'] = I('param.alarm_name','');
    		}  
			$alarmSettingTable->where($map)->save($data);
			echo json_encode(array('result'=>'修改成功','code'=>1));
		}else{
			echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
		}
	}
	
	//报警设置删除
	public function deleteAlarmSetting(){
		if(session('name')){
			$alarmSettingTable = M ( 'AlertRule' );
			$alarmSettingTable->delete(I('param.id'));
			echo json_encode(array('result'=>'删除成功','code'=>1));
		}else{
			echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
		}
	}
}